﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria.Graphics.Effects;
using Terraria.Utilities;

namespace Terraria.GameContent.Skies
{
	// Token: 0x02000410 RID: 1040
	public class MartianSky : CustomSky
	{
		// Token: 0x0600246B RID: 9323 RVA: 0x0047D870 File Offset: 0x0047BA70
		public override void Update(GameTime gameTime)
		{
			if (Main.gamePaused || !Main.hasFocus)
			{
				return;
			}
			int num = this._activeUfos;
			for (int i = 0; i < this._ufos.Length; i++)
			{
				MartianSky.Ufo ufo = this._ufos[i];
				if (ufo.IsActive)
				{
					int frame = ufo.Frame;
					ufo.Frame = frame + 1;
					if (!ufo.Update())
					{
						if (!this._leaving)
						{
							ufo.AssignNewBehavior();
						}
						else
						{
							ufo.IsActive = false;
							num--;
						}
					}
				}
				this._ufos[i] = ufo;
			}
			if (!this._leaving && num != this._maxUfos)
			{
				this._ufos[num].IsActive = true;
				this._ufos[num++].AssignNewBehavior();
			}
			this._active = (!this._leaving || num != 0);
			this._activeUfos = num;
		}

		// Token: 0x0600246C RID: 9324 RVA: 0x0047D954 File Offset: 0x0047BB54
		public override void Draw(SpriteBatch spriteBatch, float minDepth, float maxDepth)
		{
			if (Main.screenPosition.Y > 10000f)
			{
				return;
			}
			int num = -1;
			int num2 = 0;
			for (int i = 0; i < this._ufos.Length; i++)
			{
				float depth = this._ufos[i].Depth;
				if (num == -1 && depth < maxDepth)
				{
					num = i;
				}
				if (depth <= minDepth)
				{
					break;
				}
				num2 = i;
			}
			if (num == -1)
			{
				return;
			}
			Color value = new Color(Main.bgColor.ToVector4() * 0.9f + new Vector4(0.1f));
			Vector2 value2 = Main.screenPosition + new Vector2((float)(Main.screenWidth >> 1), (float)(Main.screenHeight >> 1));
			Rectangle rectangle = new Rectangle(-1000, -1000, 4000, 4000);
			for (int j = num; j < num2; j++)
			{
				Vector2 vector = new Vector2(1f / this._ufos[j].Depth, 0.9f / this._ufos[j].Depth);
				Vector2 vector2 = this._ufos[j].Position;
				vector2 = (vector2 - value2) * vector + value2 - Main.screenPosition;
				if (this._ufos[j].IsActive && rectangle.Contains((int)vector2.X, (int)vector2.Y))
				{
					spriteBatch.Draw(this._ufos[j].Texture, vector2, new Rectangle?(this._ufos[j].GetSourceRectangle()), value * this._ufos[j].Opacity, this._ufos[j].Rotation, Vector2.Zero, vector.X * 5f * this._ufos[j].Scale, SpriteEffects.None, 0f);
					if (this._ufos[j].GlowTexture != null)
					{
						spriteBatch.Draw(this._ufos[j].GlowTexture, vector2, new Rectangle?(this._ufos[j].GetSourceRectangle()), Color.White * this._ufos[j].Opacity, this._ufos[j].Rotation, Vector2.Zero, vector.X * 5f * this._ufos[j].Scale, SpriteEffects.None, 0f);
					}
				}
			}
		}

		// Token: 0x0600246D RID: 9325 RVA: 0x0047DBFC File Offset: 0x0047BDFC
		private void GenerateUfos()
		{
			float num = (float)Main.maxTilesX / 4200f;
			this._maxUfos = (int)(256f * num);
			this._ufos = new MartianSky.Ufo[this._maxUfos];
			int num2 = this._maxUfos >> 4;
			for (int i = 0; i < num2; i++)
			{
				float num3 = (float)i / (float)num2;
				this._ufos[i] = new MartianSky.Ufo(Main.extraTexture[5], (float)Main.rand.NextDouble() * 4f + 6.6f);
				this._ufos[i].GlowTexture = Main.glowMaskTexture[90];
			}
			for (int j = num2; j < this._ufos.Length; j++)
			{
				float num4 = (float)(j - num2) / (float)(this._ufos.Length - num2);
				this._ufos[j] = new MartianSky.Ufo(Main.extraTexture[6], (float)Main.rand.NextDouble() * 5f + 1.6f);
				this._ufos[j].Scale = 0.5f;
				this._ufos[j].GlowTexture = Main.glowMaskTexture[91];
			}
		}

		// Token: 0x0600246E RID: 9326 RVA: 0x0047DD28 File Offset: 0x0047BF28
		public override void Activate(Vector2 position, params object[] args)
		{
			this._activeUfos = 0;
			this.GenerateUfos();
			Array.Sort<MartianSky.Ufo>(this._ufos, (MartianSky.Ufo ufo1, MartianSky.Ufo ufo2) => ufo2.Depth.CompareTo(ufo1.Depth));
			this._active = true;
			this._leaving = false;
		}

		// Token: 0x0600246F RID: 9327 RVA: 0x00019C1E File Offset: 0x00017E1E
		public override void Deactivate(params object[] args)
		{
			this._leaving = true;
		}

		// Token: 0x06002470 RID: 9328 RVA: 0x00019C27 File Offset: 0x00017E27
		public override bool IsActive()
		{
			return this._active;
		}

		// Token: 0x06002471 RID: 9329 RVA: 0x00019C2F File Offset: 0x00017E2F
		public override void Reset()
		{
			this._active = false;
		}

		// Token: 0x04004041 RID: 16449
		private MartianSky.Ufo[] _ufos;

		// Token: 0x04004042 RID: 16450
		private UnifiedRandom _random = new UnifiedRandom();

		// Token: 0x04004043 RID: 16451
		private int _maxUfos;

		// Token: 0x04004044 RID: 16452
		private bool _active;

		// Token: 0x04004045 RID: 16453
		private bool _leaving;

		// Token: 0x04004046 RID: 16454
		private int _activeUfos;

		// Token: 0x02000411 RID: 1041
		private abstract class IUfoController
		{
			// Token: 0x06002473 RID: 9331
			public abstract void InitializeUfo(ref MartianSky.Ufo ufo);

			// Token: 0x06002474 RID: 9332
			public abstract bool Update(ref MartianSky.Ufo ufo);
		}

		// Token: 0x02000412 RID: 1042
		private class ZipBehavior : MartianSky.IUfoController
		{
			// Token: 0x06002476 RID: 9334 RVA: 0x0047DD7C File Offset: 0x0047BF7C
			public override void InitializeUfo(ref MartianSky.Ufo ufo)
			{
				ufo.Position.X = (float)(MartianSky.Ufo.Random.NextDouble() * (double)(Main.maxTilesX << 4));
				ufo.Position.Y = (float)(MartianSky.Ufo.Random.NextDouble() * 5000.0);
				ufo.Opacity = 0f;
				float num = (float)MartianSky.Ufo.Random.NextDouble() * 5f + 10f;
				double num2 = MartianSky.Ufo.Random.NextDouble() * 0.60000002384185791 - 0.30000001192092896;
				ufo.Rotation = (float)num2;
				if (MartianSky.Ufo.Random.Next(2) == 0)
				{
					num2 += 3.1415927410125732;
				}
				this._speed = new Vector2((float)Math.Cos(num2) * num, (float)Math.Sin(num2) * num);
				this._ticks = 0;
				this._maxTicks = MartianSky.Ufo.Random.Next(400, 500);
			}

			// Token: 0x06002477 RID: 9335 RVA: 0x0047DE6C File Offset: 0x0047C06C
			public override bool Update(ref MartianSky.Ufo ufo)
			{
				if (this._ticks < 10)
				{
					ufo.Opacity += 0.1f;
				}
				else if (this._ticks > this._maxTicks - 10)
				{
					ufo.Opacity -= 0.1f;
				}
				ufo.Position += this._speed;
				if (this._ticks == this._maxTicks)
				{
					return false;
				}
				this._ticks++;
				return true;
			}

			// Token: 0x04004047 RID: 16455
			private Vector2 _speed;

			// Token: 0x04004048 RID: 16456
			private int _ticks;

			// Token: 0x04004049 RID: 16457
			private int _maxTicks;
		}

		// Token: 0x02000413 RID: 1043
		private class HoverBehavior : MartianSky.IUfoController
		{
			// Token: 0x06002479 RID: 9337 RVA: 0x0047DEF0 File Offset: 0x0047C0F0
			public override void InitializeUfo(ref MartianSky.Ufo ufo)
			{
				ufo.Position.X = (float)(MartianSky.Ufo.Random.NextDouble() * (double)(Main.maxTilesX << 4));
				ufo.Position.Y = (float)(MartianSky.Ufo.Random.NextDouble() * 5000.0);
				ufo.Opacity = 0f;
				ufo.Rotation = 0f;
				this._ticks = 0;
				this._maxTicks = MartianSky.Ufo.Random.Next(120, 240);
			}

			// Token: 0x0600247A RID: 9338 RVA: 0x0047DF70 File Offset: 0x0047C170
			public override bool Update(ref MartianSky.Ufo ufo)
			{
				if (this._ticks < 10)
				{
					ufo.Opacity += 0.1f;
				}
				else if (this._ticks > this._maxTicks - 10)
				{
					ufo.Opacity -= 0.1f;
				}
				if (this._ticks == this._maxTicks)
				{
					return false;
				}
				this._ticks++;
				return true;
			}

			// Token: 0x0400404A RID: 16458
			private int _ticks;

			// Token: 0x0400404B RID: 16459
			private int _maxTicks;
		}

		// Token: 0x02000414 RID: 1044
		private struct Ufo
		{
			// Token: 0x17000295 RID: 661
			// (get) Token: 0x0600247C RID: 9340 RVA: 0x00019C53 File Offset: 0x00017E53
			// (set) Token: 0x0600247D RID: 9341 RVA: 0x00019C5B File Offset: 0x00017E5B
			public int Frame
			{
				get
				{
					return this._frame;
				}
				set
				{
					this._frame = value % 12;
				}
			}

			// Token: 0x17000296 RID: 662
			// (get) Token: 0x0600247E RID: 9342 RVA: 0x00019C67 File Offset: 0x00017E67
			// (set) Token: 0x0600247F RID: 9343 RVA: 0x00019C6F File Offset: 0x00017E6F
			public Texture2D Texture
			{
				get
				{
					return this._texture;
				}
				set
				{
					this._texture = value;
					this.FrameWidth = value.Width;
					this.FrameHeight = value.Height / 3;
				}
			}

			// Token: 0x17000297 RID: 663
			// (get) Token: 0x06002480 RID: 9344 RVA: 0x00019C92 File Offset: 0x00017E92
			// (set) Token: 0x06002481 RID: 9345 RVA: 0x00019C9A File Offset: 0x00017E9A
			public MartianSky.IUfoController Controller
			{
				get
				{
					return this._controller;
				}
				set
				{
					this._controller = value;
					value.InitializeUfo(ref this);
				}
			}

			// Token: 0x06002482 RID: 9346 RVA: 0x0047DFD8 File Offset: 0x0047C1D8
			public Ufo(Texture2D texture, float depth = 1f)
			{
				this._frame = 0;
				this.Position = Vector2.Zero;
				this._texture = texture;
				this.Depth = depth;
				this.Scale = 1f;
				this.FrameWidth = texture.Width;
				this.FrameHeight = texture.Height / 3;
				this.GlowTexture = null;
				this.Opacity = 0f;
				this.Rotation = 0f;
				this.IsActive = false;
				this._controller = null;
			}

			// Token: 0x06002483 RID: 9347 RVA: 0x00019CAA File Offset: 0x00017EAA
			public Rectangle GetSourceRectangle()
			{
				return new Rectangle(0, this._frame / 4 * this.FrameHeight, this.FrameWidth, this.FrameHeight);
			}

			// Token: 0x06002484 RID: 9348 RVA: 0x00019CCD File Offset: 0x00017ECD
			public bool Update()
			{
				return this.Controller.Update(ref this);
			}

			// Token: 0x06002485 RID: 9349 RVA: 0x0047E058 File Offset: 0x0047C258
			public void AssignNewBehavior()
			{
				int num = MartianSky.Ufo.Random.Next(2);
				if (num == 0)
				{
					this.Controller = new MartianSky.ZipBehavior();
					return;
				}
				if (num != 1)
				{
					return;
				}
				this.Controller = new MartianSky.HoverBehavior();
			}

			// Token: 0x0400404C RID: 16460
			private const int MAX_FRAMES = 3;

			// Token: 0x0400404D RID: 16461
			private const int FRAME_RATE = 4;

			// Token: 0x0400404E RID: 16462
			public static UnifiedRandom Random = new UnifiedRandom();

			// Token: 0x0400404F RID: 16463
			private int _frame;

			// Token: 0x04004050 RID: 16464
			private Texture2D _texture;

			// Token: 0x04004051 RID: 16465
			private MartianSky.IUfoController _controller;

			// Token: 0x04004052 RID: 16466
			public Texture2D GlowTexture;

			// Token: 0x04004053 RID: 16467
			public Vector2 Position;

			// Token: 0x04004054 RID: 16468
			public int FrameHeight;

			// Token: 0x04004055 RID: 16469
			public int FrameWidth;

			// Token: 0x04004056 RID: 16470
			public float Depth;

			// Token: 0x04004057 RID: 16471
			public float Scale;

			// Token: 0x04004058 RID: 16472
			public float Opacity;

			// Token: 0x04004059 RID: 16473
			public bool IsActive;

			// Token: 0x0400405A RID: 16474
			public float Rotation;
		}
	}
}
